package xby.study.proxy.cglib.proxyrefdispatcher;

import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.ProxyRefDispatcher;
import xby.study.proxy.cglib.PathUtil;

public class ProxyRefDispatcherTest {
    public void test() {
        System.out.println(this.getClass().getName());
    }

    public static void main(String[] args) {
        // 配置class文件的输出目录
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, PathUtil.getOutPath(ProxyRefDispatcherTest.class));

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(ProxyRefDispatcherTest.class);
        enhancer.setCallback(new ProxyRefDispatcher() {

            @Override
            public Object loadObject(Object o) throws Exception {
                System.out.println("内部o的类型：" + o.getClass());
                return new SonProxyRefDispatcherTest();
            }
        });
        ProxyRefDispatcherTest test = (ProxyRefDispatcherTest) enhancer.create();
        test.test();
    }
}


class SonProxyRefDispatcherTest extends ProxyRefDispatcherTest {
    public void test() {
        System.out.println("before method ...");
        System.out.println(this.getClass().getName());
        System.out.println("before method ...");
    }
}
